//
//  QuickLoginViewController.m
//  SG
//
//  Created by FISH on 2018/4/19.
//  Copyright © 2018年 yimindai. All rights reserved.
//

#import "QuickLoginViewController.h"
#import "LoginViewController.h"
@interface QuickLoginViewController ()<YBAttributeTapActionDelegate>

@end

@implementation QuickLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;

    UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524119220568&di=2501ebb437587e2c18f45aca48ad8495&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2Fd009b3de9c82d158b62f49ef890a19d8bc3e423a.jpg"]]];
    UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524119220633&di=5ef0466a9ca7464cbab4fcd0ce7d79ee&imgtype=0&src=http%3A%2F%2Feasyread.ph.126.net%2FwHNL6V_v5q6dqAQVeRTGSw%3D%3D%2F7916890539291530634.jpg"]]];
    UIImage *image3 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524119220631&di=047ca7ad2e4c9fb93f80f6593ccda8ef&imgtype=0&src=http%3A%2F%2Fpaper.mm111.net%2Fpaperdata%2Fmmwb%2F20160126%2Ffb32c9aa59434aa54ac31e9562312f54.jpg"]]];
    NSMutableArray *imageNames = [NSMutableArray array];
    [imageNames addObject:image1];
    [imageNames addObject:image2];
    [imageNames addObject:image3];
    
    
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth,  kScreenHeight-kRealValue(150)) delegate:self placeholderImage:nil];
    _cycleScrollView.backgroundColor = KYellowColor;
    _cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
    _cycleScrollView.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
    _cycleScrollView.localizationImageNamesGroup = imageNames;// 网络图片
    _cycleScrollView.autoScrollTimeInterval = 2.0;//轮播时间间隔，默认1.0秒，可自定义
    [self.view addSubview:_cycleScrollView];
    
    
    [self createQuickLoginViewControllerUI];
}

-(void)createQuickLoginViewControllerUI
{
    NSArray *btnArray = @[@"微信",@"QQ",@"手机"];
    _btnView.frame = CGRectMake(0, kScreenHeight-kRealValue(150), kScreenWidth, kRealValue(100));
    _promptLab.frame = CGRectMake((kScreenWidth-100)/2, 5, 100, 21);
    _promptLab.font = FONTTHRE;
    _leftView.frame = CGRectMake(10, 10, (kScreenWidth-120)/2, 1);
    _rightView.frame = CGRectMake(_promptLab.frame.origin.x+_promptLab.frame.size.width, 10, (kScreenWidth-120)/2, 1);
    
    int btnWidth = kRealValue(100)-36;
    int btnInterval = (kScreenWidth - btnArray.count*btnWidth)/(btnArray.count+1);
    for(int tag = 0;tag < btnArray.count;tag++)
    {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(btnInterval*(tag+1)+btnWidth*tag, _promptLab.frame.origin.y+_promptLab.frame.size.height+10, btnWidth, btnWidth)];
        [btn setTitle:StringFormat(@"%@", btnArray[tag]) forState:UIControlStateNormal];
        btn.tag = tag;
        [btn addTarget:self action:@selector(toSelectInterface:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = KLightGrayColor;
        [_btnView addSubview:btn];
    }
    
    NSString *intStr = @"登录或注册即表示同意遵守 《搜缘使用协议》";
    NSDictionary *intDic=@{NSFontAttributeName:FONTONE,NSForegroundColorAttributeName:KLightGrayColor};
    NSMutableAttributedString *intAbuted = [[NSMutableAttributedString alloc]initWithString:intStr attributes:intDic];
    [intAbuted addAttribute:NSFontAttributeName value:FONTONE range:NSMakeRange(intStr.length-8, 8)];//变大
    [intAbuted addAttribute:NSForegroundColorAttributeName value:KRedColor range:NSMakeRange(intStr.length-8, 8)];
    CGSize labSize = [self sizeWithString:intStr font:FONTONE maxWidth:kScreenWidth];
    int labelX = (kScreenWidth - labSize.width)/2;
    _agreementLab.frame = CGRectMake(labelX,  _btnView.frame.origin.y+_btnView.frame.size.height, labSize.width, 21);
    _agreementLab.attributedText = intAbuted;
    [_agreementLab yb_addAttributeTapActionWithStrings:@[@"《搜缘使用协议》"] delegate:self];
}

- (void)toSelectInterface:(UIButton*)sender
{
    if(sender.tag == 0)
    {
        [self getAuthWithUserInfoFromWechat];
    }
    else if (sender.tag == 1)
    {
        [self getAuthWithUserInfoFromQQ];
    }
    else if (sender.tag == 2)
    {
        LoginViewController *LoginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:LoginVC animated:NO];
    }
}

- (void)getAuthWithUserInfoFromQQ
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            DLog(@"%@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            DLog(@"QQ uid: %@", resp.uid);
            DLog(@"QQ openid: %@", resp.openid);
            DLog(@"QQ unionid: %@", resp.unionId);
            DLog(@"QQ accessToken: %@", resp.accessToken);
            DLog(@"QQ expiration: %@", resp.expiration);
            
            // 用户信息
            DLog(@"QQ name: %@", resp.name);
            DLog(@"QQ iconurl: %@", resp.iconurl);
            DLog(@"QQ gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            DLog(@"QQ originalResponse: %@", resp.originalResponse);
        }
    }];
}

- (void)getAuthWithUserInfoFromWechat
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            DLog(@"%@",error);
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            DLog(@"Wechat uid: %@", resp.uid);
            DLog(@"Wechat openid: %@", resp.openid);
            DLog(@"Wechat unionid: %@", resp.unionId);
            DLog(@"Wechat accessToken: %@", resp.accessToken);
            DLog(@"Wechat refreshToken: %@", resp.refreshToken);
            DLog(@"Wechat expiration: %@", resp.expiration);
            
            // 用户信息
            DLog(@"Wechat name: %@", resp.name);
            DLog(@"Wechat iconurl: %@", resp.iconurl);
            DLog(@"Wechat gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            DLog(@"Wechat originalResponse: %@", resp.originalResponse);
        }
    }];
}

- (void)yb_attributeTapReturnString:(NSString *)string range:(NSRange)range index:(NSInteger)index
{
    if ([string isEqualToString:@"《搜缘使用协议》"])
    {
        DLog(@"《搜缘使用协议》");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
